using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Granite.Eventing
{
/// <summary>
/// The object that is referenced by the eventHandler must keep a reference to this class. 
/// </summary>
public class Listener<T> : IListener<T> where T : EventArgs
{
	readonly Action<object, T> m_EventHandler;

	/// <summary>
	/// The object that is referenced by the eventHandler must keep a reference to this class. 
	/// </summary>
	public Listener(Action<object, T> eventHandler)
	{
		if (eventHandler == null)
			throw new ArgumentNullException("eventHandler", "eventHandler is null.");
		Contract.EndContractBlock();

		m_EventHandler = eventHandler;
	}

	/// <summary>
	/// Invokes the assocated delegate
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	public void Invoke(object sender, T e)
	{
		m_EventHandler(sender, e);
	}

	[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
	[ContractInvariantMethod]
	void ObjectInvariant()
	{
		Contract.Invariant(m_EventHandler != null);
	}
}
}
